package com.blog.service.Impl;

import com.blog.redis.RedisUtil;
import com.blog.service.SysCacheService;

import com.blog.util.dto.AjaxResult;
import org.springframework.data.redis.connection.DataType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static org.springframework.data.redis.connection.DataType.*;

/**
 * @author cgx
 * @Date 2021/9/30 9:21
 */
@Service
public class SysCacheServiceImpl implements SysCacheService {

    @Resource
    RedisUtil redisUtil;


    @Override
    public AjaxResult queryCacheValue(String redisKey) {
        DataType type = redisUtil.type(redisKey);
            //list类型
        if (type == LIST){
            List<Object> list = redisUtil.lGet(redisKey, 0, -1);
            if (null != list){
                if (!list.isEmpty()){
                    return AjaxResult.success(list,"LIST");
                }else {
                    return AjaxResult.error();
                }
            }else {
                return AjaxResult.error();
            }
            //string类型
        }else if (type == STRING){
            Object redisValue = redisUtil.get(redisKey);
            if (redisValue != null || !redisKey.isEmpty()){
                return AjaxResult.success(redisValue,"STRING");
            }else {
                return AjaxResult.error();
            }
            //set类型
        }else if (type == SET){
            Set<Object> objects = redisUtil.sGet(redisKey);
            if (null != objects){
                if (!objects.isEmpty()){
                    return AjaxResult.success(objects,"SET");
                }else {
                    return AjaxResult.error();
                }
            }else {
                return AjaxResult.error();
            }
            //hash、zset、none不常用，先不写
        }else if (type == HASH){
            return null;
        }else if (type == ZSET){
            return null;
        }else if (type == NONE){
            return null;
        }else {
            return null;
        }
    }
}
